package GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JProgressBar;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.Integer;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import single.singlethreaded;
import multi.multithread;

public class lafenetre {

	private JFrame frame;
	private JTextField callorput;
	private JTextField currentprice;
	private JTextField strikeprice;
	private JTextField simulations;
	private JTextField steps;
	private JTextField maturity;
	private JTextField discount;
	private JTextField carry;
	private JTextField volatility;
	private JSeparator separator;
	private JComboBox comboBox;
	private JLabel lblResultat;
	private JLabel resultat;
	private JLabel lblNewLabel_9;
	
	public int nthread;

	
	private double running() throws NumberFormatException, InterruptedException, ExecutionException{
		
		double result = 0;
		
		switch(nthread){
		
		case 1 : 
				result = singlethreadedinstance.MonteCarloStandardOption(callorput.getText(), Double.parseDouble(currentprice.getText()), 
				Double.parseDouble(strikeprice.getText()), Double.parseDouble(maturity.getText()), 
				Double.parseDouble(discount.getText()), Double.parseDouble(carry.getText()), 
				Double.parseDouble(volatility.getText()), Integer.parseInt(steps.getText()), 
				Integer.parseInt(simulations.getText()));
				break;
				
		case 2 : 				result = multithread.MonteCarloStandardOption(callorput.getText(), Double.parseDouble(currentprice.getText()), 
				Double.parseDouble(strikeprice.getText()), Double.parseDouble(maturity.getText()), 
				Double.parseDouble(discount.getText()), Double.parseDouble(carry.getText()), 
				Double.parseDouble(volatility.getText()), Integer.parseInt(steps.getText()), 
				Integer.parseInt(simulations.getText()));
		break;
		}
		return result;
	}
	
	public void nexterization(File file){
		try {
			Scanner scan = new Scanner(file);
			callorput.setText(scan.next());
			currentprice.setText(scan.next());
			strikeprice.setText(scan.next());
			simulations.setText(scan.next());
			steps.setText(scan.next());
			maturity.setText(scan.next());
			discount.setText(scan.next());
			carry.setText(scan.next());
			volatility.setText(scan.next());
			
			scan.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					lafenetre window = new lafenetre();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public lafenetre() {
		initialize();
	}
	
	singlethreaded singlethreadedinstance = new singlethreaded(this);
	multithread multithreadedinstance = new multithread (this);
	private JComboBox nthreads;
	private JLabel lblPresets;
	private JLabel lblNOfThreads;
	private JLabel label;

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 535, 335);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 138, 0, 80, 0};
		gridBagLayout.rowHeights = new int[]{19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		frame.getContentPane().setLayout(gridBagLayout);
		
		final JButton btnRun = new JButton("RUN");
		btnRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				final long start = System.nanoTime();
				
				double result = 0;
				try {
					result = running();
				} catch (NumberFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ExecutionException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				final long end = System.nanoTime();
				
				label.setText(Long.toString((long) (end - start)/1000000));
				resultat.setText(Double.toString(result));
			}
		});
		GridBagConstraints gbc_btnRun = new GridBagConstraints();
		gbc_btnRun.insets = new Insets(0, 0, 5, 5);
		gbc_btnRun.gridx = 1;
		gbc_btnRun.gridy = 0;
		frame.getContentPane().add(btnRun, gbc_btnRun);
		
		separator = new JSeparator();
		GridBagConstraints gbc_separator = new GridBagConstraints();
		gbc_separator.insets = new Insets(0, 0, 5, 5);
		gbc_separator.gridx = 2;
		gbc_separator.gridy = 0;
		frame.getContentPane().add(separator, gbc_separator);
		
		JLabel lblNewLabel = new JLabel("Specifies whether the option is a call or put option :");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 1;
		frame.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		callorput = new JTextField();
		callorput.setText("c");
		GridBagConstraints gbc_callorput = new GridBagConstraints();
		gbc_callorput.fill = GridBagConstraints.HORIZONTAL;
		gbc_callorput.insets = new Insets(0, 0, 5, 5);
		gbc_callorput.gridx = 1;
		gbc_callorput.gridy = 1;
		frame.getContentPane().add(callorput, gbc_callorput);
		callorput.setColumns(10);
		
		lblPresets = new JLabel("Presets :");
		GridBagConstraints gbc_lblPresets = new GridBagConstraints();
		gbc_lblPresets.anchor = GridBagConstraints.WEST;
		gbc_lblPresets.insets = new Insets(0, 0, 5, 0);
		gbc_lblPresets.gridx = 3;
		gbc_lblPresets.gridy = 1;
		frame.getContentPane().add(lblPresets, gbc_lblPresets);
		
		JLabel lblNewLabel_1 = new JLabel("Current price of the underlying asset :");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 2;
		frame.getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		currentprice = new JTextField();
		currentprice.setText("0");
		GridBagConstraints gbc_currentprice = new GridBagConstraints();
		gbc_currentprice.insets = new Insets(0, 0, 5, 5);
		gbc_currentprice.fill = GridBagConstraints.HORIZONTAL;
		gbc_currentprice.gridx = 1;
		gbc_currentprice.gridy = 2;
		frame.getContentPane().add(currentprice, gbc_currentprice);
		currentprice.setColumns(10);
		
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String select = comboBox.getSelectedItem().toString();
				
				switch(select){
					case "Set 1" : 
						// Load file
						File preset1 = new File("preset1.txt");
						nexterization(preset1);
						
						break;
					
					case "Set 2" : 
						// Load file
						File preset2 = new File("preset2.txt");
						nexterization(preset2);
						break;
				
					case "Set 3" : 
						// Load file
						File preset3 = new File("preset3.txt");
						nexterization(preset3);
						break;
					
					case "Set 4" : 
						File preset4 = new File("preset4.txt");
						nexterization(preset4);
						break;
					
					case "Set 5" : 
						File preset5 = new File("preset5.txt");
						nexterization(preset5);
						break;
						
					default :
						File def = new File("default.txt");
						nexterization(def);
						break;
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"No set", "Set 1", "Set 2", "Set 3", "Set 4", "Set 5"}));
		comboBox.setMaximumRowCount(6);
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 3;
		gbc_comboBox.gridy = 2;
		frame.getContentPane().add(comboBox, gbc_comboBox);
		
		JLabel lblNewLabel_2 = new JLabel("Strike price :");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 3;
		frame.getContentPane().add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		strikeprice = new JTextField();
		strikeprice.setText("0");
		GridBagConstraints gbc_strikeprice = new GridBagConstraints();
		gbc_strikeprice.insets = new Insets(0, 0, 5, 5);
		gbc_strikeprice.fill = GridBagConstraints.HORIZONTAL;
		gbc_strikeprice.gridx = 1;
		gbc_strikeprice.gridy = 3;
		frame.getContentPane().add(strikeprice, gbc_strikeprice);
		strikeprice.setColumns(10);
		
		lblNOfThreads = new JLabel("N. of threads :");
		GridBagConstraints gbc_lblNOfThreads = new GridBagConstraints();
		gbc_lblNOfThreads.anchor = GridBagConstraints.WEST;
		gbc_lblNOfThreads.insets = new Insets(0, 0, 5, 0);
		gbc_lblNOfThreads.gridx = 3;
		gbc_lblNOfThreads.gridy = 3;
		frame.getContentPane().add(lblNOfThreads, gbc_lblNOfThreads);
		
		JLabel lblNewLabel_3 = new JLabel("Time to maturity (in years) :");
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_3.gridx = 0;
		gbc_lblNewLabel_3.gridy = 4;
		frame.getContentPane().add(lblNewLabel_3, gbc_lblNewLabel_3);
		
		maturity = new JTextField();
		maturity.setText("0");
		GridBagConstraints gbc_maturity = new GridBagConstraints();
		gbc_maturity.insets = new Insets(0, 0, 5, 5);
		gbc_maturity.fill = GridBagConstraints.HORIZONTAL;
		gbc_maturity.gridx = 1;
		gbc_maturity.gridy = 4;
		frame.getContentPane().add(maturity, gbc_maturity);
		maturity.setColumns(10);
		
		nthreads = new JComboBox();
		nthreads.setModel(new DefaultComboBoxModel(new String[] {"select...", "singlethreaded", "multithreaded"}));
		nthreads.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String select = nthreads.getSelectedItem().toString();
				
				switch(select){
				case "singlethreaded" : nthread = 1;
										break;
										
				case "multithreaded" : 	nthread = 2;
										break;
				}
			}
		});
		GridBagConstraints gbc_nthreads = new GridBagConstraints();
		gbc_nthreads.insets = new Insets(0, 0, 5, 0);
		gbc_nthreads.fill = GridBagConstraints.HORIZONTAL;
		gbc_nthreads.gridx = 3;
		gbc_nthreads.gridy = 4;
		frame.getContentPane().add(nthreads, gbc_nthreads);
		
		JLabel lblNewLabel_4 = new JLabel("Discount rate (risk-free interest rate) :");
		GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
		gbc_lblNewLabel_4.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_4.gridx = 0;
		gbc_lblNewLabel_4.gridy = 5;
		frame.getContentPane().add(lblNewLabel_4, gbc_lblNewLabel_4);
		
		discount = new JTextField();
		discount.setText("0");
		GridBagConstraints gbc_discount = new GridBagConstraints();
		gbc_discount.insets = new Insets(0, 0, 5, 5);
		gbc_discount.fill = GridBagConstraints.HORIZONTAL;
		gbc_discount.gridx = 1;
		gbc_discount.gridy = 5;
		frame.getContentPane().add(discount, gbc_discount);
		discount.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("Cost of carry rate :");
		GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
		gbc_lblNewLabel_5.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_5.gridx = 0;
		gbc_lblNewLabel_5.gridy = 6;
		frame.getContentPane().add(lblNewLabel_5, gbc_lblNewLabel_5);
		
		carry = new JTextField();
		carry.setText("0");
		GridBagConstraints gbc_carry = new GridBagConstraints();
		gbc_carry.insets = new Insets(0, 0, 5, 5);
		gbc_carry.fill = GridBagConstraints.HORIZONTAL;
		gbc_carry.gridx = 1;
		gbc_carry.gridy = 6;
		frame.getContentPane().add(carry, gbc_carry);
		carry.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("Volatility of the underlying asset :");
		GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
		gbc_lblNewLabel_6.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_6.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_6.gridx = 0;
		gbc_lblNewLabel_6.gridy = 7;
		frame.getContentPane().add(lblNewLabel_6, gbc_lblNewLabel_6);
		
		volatility = new JTextField();
		volatility.setText("0");
		GridBagConstraints gbc_volatility = new GridBagConstraints();
		gbc_volatility.insets = new Insets(0, 0, 5, 5);
		gbc_volatility.fill = GridBagConstraints.HORIZONTAL;
		gbc_volatility.gridx = 1;
		gbc_volatility.gridy = 7;
		frame.getContentPane().add(volatility, gbc_volatility);
		volatility.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("Number of steps to execute for each option :");
		GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
		gbc_lblNewLabel_7.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_7.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_7.gridx = 0;
		gbc_lblNewLabel_7.gridy = 8;
		frame.getContentPane().add(lblNewLabel_7, gbc_lblNewLabel_7);
		
		steps = new JTextField();
		steps.setText("0");
		GridBagConstraints gbc_steps = new GridBagConstraints();
		gbc_steps.insets = new Insets(0, 0, 5, 5);
		gbc_steps.fill = GridBagConstraints.HORIZONTAL;
		gbc_steps.gridx = 1;
		gbc_steps.gridy = 8;
		frame.getContentPane().add(steps, gbc_steps);
		steps.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel("Number of simulations to run :");
		GridBagConstraints gbc_lblNewLabel_8 = new GridBagConstraints();
		gbc_lblNewLabel_8.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_8.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_8.gridx = 0;
		gbc_lblNewLabel_8.gridy = 9;
		frame.getContentPane().add(lblNewLabel_8, gbc_lblNewLabel_8);
		
		simulations = new JTextField();
		simulations.setText("0");
		GridBagConstraints gbc_simulations = new GridBagConstraints();
		gbc_simulations.insets = new Insets(0, 0, 5, 5);
		gbc_simulations.fill = GridBagConstraints.HORIZONTAL;
		gbc_simulations.gridx = 1;
		gbc_simulations.gridy = 9;
		frame.getContentPane().add(simulations, gbc_simulations);
		simulations.setColumns(10);
		
		lblResultat = new JLabel("Result :");
		GridBagConstraints gbc_lblResultat = new GridBagConstraints();
		gbc_lblResultat.anchor = GridBagConstraints.EAST;
		gbc_lblResultat.insets = new Insets(0, 0, 5, 5);
		gbc_lblResultat.gridx = 0;
		gbc_lblResultat.gridy = 10;
		frame.getContentPane().add(lblResultat, gbc_lblResultat);
		
		resultat = new JLabel("?");
		GridBagConstraints gbc_resultat = new GridBagConstraints();
		gbc_resultat.insets = new Insets(0, 0, 5, 5);
		gbc_resultat.gridx = 1;
		gbc_resultat.gridy = 10;
		frame.getContentPane().add(resultat, gbc_resultat);
		
		lblNewLabel_9 = new JLabel("Calculation time (en milliseconde) :");
		GridBagConstraints gbc_lblNewLabel_9 = new GridBagConstraints();
		gbc_lblNewLabel_9.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_9.insets = new Insets(0, 0, 0, 5);
		gbc_lblNewLabel_9.gridx = 0;
		gbc_lblNewLabel_9.gridy = 11;
		frame.getContentPane().add(lblNewLabel_9, gbc_lblNewLabel_9);
		
		label = new JLabel("?");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.insets = new Insets(0, 0, 0, 5);
		gbc_label.gridx = 1;
		gbc_label.gridy = 11;
		frame.getContentPane().add(label, gbc_label);
	}
}
